package com.gxks.lhs.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MybatisMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        // 获取当前用户的用户名
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        // 设置更新人字段的值
//        this.setFieldValByName("createUser", username, metaObject);
//        this.setFieldValByName("updateUser", username, metaObject);
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        // 获取当前用户的用户名
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        // 设置更新人字段的值
//        this.setFieldValByName("updateUser", username, metaObject);
    }
}
